﻿
using RM.BasicLibrary.Models.Repositorys;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity;

namespace RM.BasicLibrary.Models.Databases
{
    /// <summary>
    /// 
    /// </summary>
    public interface IDatabase
    {

        #region ado.net
        string ConnectionString { get; set; }
        /// <summary>
        /// Set the connection string
        /// </summary>
        /// <param name="connectionString">connection string</param>
        void SetDatabase(string connectionString);
        /// <summary>
        /// Execute sql, return the affected row
        /// </summary>
        /// <param name="cmdText">sql command text</param>
        /// <param name="type">command type</param>
        /// <param name="parm">Parameter Collection </param>
        int ExecuteNonQuery(string cmdText, CommandType type = CommandType.Text, params DbParameter[] parm);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="type"></param>
        /// <param name="parm"></param>
        /// <returns></returns>
        object ExecuteScalar(string cmdText, CommandType type = CommandType.Text, params DbParameter[] parm);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="type"></param>
        /// <param name="parm"></param>
        /// <returns></returns>
        DbDataReader ExecuteReader(string cmdText, CommandType type = CommandType.Text, params DbParameter[] parm);
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmdText"></param>
        /// <param name="type"></param>
        /// <param name="parm"></param>
        /// <returns></returns>
        T ExecuteReader<T>(string cmdText, CommandType type = CommandType.Text, params DbParameter[] parm) where T : new();
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmdText"></param>
        /// <param name="type"></param>
        /// <param name="parm"></param>
        /// <returns></returns>
        IEnumerable<T> ExecuteReaders<T>(string cmdText, CommandType type = CommandType.Text, params DbParameter[] parm) where T : new();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="type"></param>
        /// <param name="parm"></param>
        /// <returns></returns>
        DataTable GetDataTable(string cmdText, CommandType type = CommandType.Text, params DbParameter[] parm);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tranAction"></param>
        void Transaction(Action<DbCommand> tranAction);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tranAction"></param>
        dynamic Execute(Func<DbCommand, dynamic> tranAction);
        #endregion

        /// <summary>
        /// 获取 当前使用的数据访问上下文对象
        /// </summary>
        DbContext DBcontext { get; set; }
        DbContext CreateDbContext(string connectionString = null);
    }
    public interface IDBContext
    {
        Action<DbModelBuilder> OnModelCreate { get; set; }
    }
}
